package ru.usu.gv.styles;

import java.awt.Color;

/**
 * @author spupyrev
 * 02.11.2008
 */
public class StyleUtilities
{
	public static Color decodeColor(String color)
	{
		if (color.startsWith("#"))
			color = color.substring(1);
		int r = Integer.parseInt(color.substring(0, 2), 16);
		int g = Integer.parseInt(color.substring(2, 4), 16);
		int b = Integer.parseInt(color.substring(4, 6), 16);
		return new Color(r, g, b);
	}

	public static String encodeColor(Color color)
	{
		String red = extractComponentColor(color.getRed());
		String green = extractComponentColor(color.getGreen());
		String blue = extractComponentColor(color.getBlue());
		return "#" + red + green + blue;
	}

	private static String extractComponentColor(int component)
	{
		String red = Integer.toString(component, 16);
		while (red.length() < 2)
			red = "0" + red;
		return red;
	}

}
